package uiLayer;

import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import modelLayer.Block;
import modelLayer.TeachingBlock;

public class ScheduleTable extends JTable {
	private static final long serialVersionUID = 1L;
	private CustomTableModel tableModel;
	
	public ScheduleTable() {
		super();
		tableModel = new CustomTableModel();
		this.setModel(tableModel);
		//tableModel.setColNames("Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag");
		super.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
		super.setRowSelectionAllowed(false);
		super.setCellSelectionEnabled(true);
		//super.setDragEnabled(true);
	}
	
	public void refresh(){
		tableModel.fireTableDataChanged();
	}
	
	public List<Block> getDataModel(){
		return tableModel.model;
	}
	
	public boolean doIsCellEditable(int rowIndex, int columnIndex){
		//return columnIndex >= 0;
		return false;
	}

	public void doSetValueAt(Object aValue, int row, int col){
		int numberOfBlocksPerDay = 7;
		int indexPosition = col * numberOfBlocksPerDay + row;  
		Block b = this.tableModel.getObjectByIndex(indexPosition);
		b.setTeachingBlock((TeachingBlock)aValue);
	}

	public final void setModel(List<Block> objectList) {
		this.tableModel.setModel(objectList);
	}

	public Object getCellValue(int col, int row, List<Block> model){
		Object res = null;
		int numberOfBlocksPerDay = 7;
		int position = col * numberOfBlocksPerDay + row;  
		if (model.get(position).getTeachingBlock() != null){
			res = model.get(position).getTeachingBlock();
		}
		else {
			res = model.get(position);
		}
		return res;
	}

	public final Block getSelectedObject() {
		int numberOfBlocksPerDay = 7;
		int row = getSelectedRow();
		int col = getSelectedColumn();
		if (row < 0){
			return null;
		}
		return this.tableModel.getObjectByIndex(col * numberOfBlocksPerDay + row);
	}

	//AbstractTableModel inner class
	private class CustomTableModel extends AbstractTableModel{
		private static final long serialVersionUID = 1L;
		
		private List<Block> model = new ArrayList<>();
		private String[] colNames = {"Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag"};
		
		public void setModel(List<Block> model){
			//System.out.println("AbstractCustomTable.CustomTableModel.setModel()");
			this.model = model;
			super.fireTableDataChanged();
		}

//		public void setColNames(String... colNames){
//			this.colNames = colNames;
//			super.fireTableStructureChanged();
//		}
		
		@Override
		public String getColumnName(int column) {
			//System.out.println("AbstractCustomTable.CustomTableModel.getColumnName(" + column + ")");
			return colNames[column];
		}

		@Override
		public int getColumnCount() {
			//System.out.println("AbstractCustomTable.CustomTableModel.getColumnCount() --> " + colNames.length);
			return colNames.length;
		}

		@Override
		public int getRowCount() {
			//System.out.println("AbstractCustomTable.CustomTableModel.getRowCount() --> " + model.size() );
			//return model.size();
			return (model.size() / 5);
		}

		@Override
		public Object getValueAt(int row, int col) {
			//System.out.println("AbstractCustomTable.CustomTableModel.getValueAt(" + row + ", " + col + ")");
			//return getCellValue(col, model.get(row));
			return getCellValue(col, row, model);
		}

		public Block getObjectByIndex(int indexPosition) {
			return model.get(indexPosition);
		}
		
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			//return doGetColumnClass(columnIndex);
			return getValueAt(0, columnIndex).getClass();
		}
		
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return doIsCellEditable(rowIndex, columnIndex);
		}
		
		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			doSetValueAt(aValue, rowIndex, columnIndex);
			fireTableCellUpdated(rowIndex, columnIndex);
		}
	}
}
